package com.wanmait.recycle.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.recycle.mapper.RubbishMapper;
import com.wanmait.recycle.mapper.SecondtypeMapper;
import com.wanmait.recycle.pojo.Admin;
import com.wanmait.recycle.pojo.Firsttype;
import com.wanmait.recycle.pojo.Rubbish;
import com.wanmait.recycle.pojo.RubbishUnit;
import com.wanmait.recycle.pojo.Secondtype;
import com.wanmait.recycle.pojo.Userinfo;
import com.wanmait.recycle.service.ConfigService;
import com.wanmait.recycle.service.SecondtypeService;
import com.wanmait.recycle.util.PageContext;
import com.wanmait.recycle.util.Pager;

@Service

public class SecondtypeServiceImpl implements SecondtypeService {

	@Resource
	private SecondtypeMapper secondtypeMapper;
	@Resource
	private RubbishMapper rubbishMapper;
	@Resource
	private ConfigService configService;
	
	@Override
	public void insert(Secondtype object) {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(Secondtype object) {
		// TODO Auto-generated method stub

	}

	@Override
	public void delete(Integer id) {
		// TODO Auto-generated method stub

	}

	@Override
	public void delete(String ids) {
		// TODO Auto-generated method stub

	}

	@Override
	public Secondtype findById(Integer id) {
		// TODO Auto-generated method stub
		return secondtypeMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<Secondtype> findAll() {
		// TODO Auto-generated method stub
		return secondtypeMapper.findAll();
	}

	@Override
	public int getDataCount() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Secondtype> findAll(Pager pager) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Secondtype> findAll(Secondtype object) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getDataCount(Secondtype object) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Secondtype> findAll(Pager pager, Secondtype object) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PageInfo<Secondtype> findAllByPager() {
		Map<String, Map<String,String>> config = configService.findAllConfig();
		PageHelper.startPage(PageContext.getPageNum(), Integer.parseInt(config.get("后台第二类型分页").get("mSecondPage")));
		
		List<Secondtype> secondtypes = secondtypeMapper.findAll();
		
		PageInfo<Secondtype> pageInfo = new PageInfo<Secondtype>(secondtypes,7);
		
		return pageInfo;
	}

	@Override
	public void deleteById(Integer id) {
		// TODO Auto-generated method stub
		secondtypeMapper.deleteById(id);
	}

	@Override
	public void deleteByIds(String ids) {
		// TODO Auto-generated method stub
		ids = ids.substring(0, ids.length()-1);
		String[] nn =ids.split(",");
		
		
		secondtypeMapper.deleteByIds(nn);
	}

	@Override
	public void insertById(Integer id) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void recallById(Integer id) {
		// TODO Auto-generated method stub
		secondtypeMapper.recallById(id);
	}

	@Override
	public PageInfo<Secondtype> findAllBySearch(String searchStr,String beginTime, String endTime) {
		Map<String, Map<String,String>> config = configService.findAllConfig();
		PageHelper.startPage(PageContext.getPageNum(),Integer.parseInt(config.get("后台第二类型分页").get("mSecondPage")));
		List<Secondtype> scs;
		if("可回收垃圾".equals(searchStr)||"可回收垃".equals(searchStr)||"可回收".equals(searchStr)||"可回".equals(searchStr)||"可".equals(searchStr)) {
			 scs = secondtypeMapper.findAllBySearch(searchStr, beginTime, endTime);
		}
		else {
			 scs = secondtypeMapper.findAllBySearchRecyclable(searchStr, beginTime, endTime);}
		
		PageInfo<Secondtype> pageInfo = new PageInfo<Secondtype>(scs);
		
		return pageInfo;
	}
	@Override
	public PageInfo<Secondtype> findAllByPrimarykey(Integer nScId) {
		Map<String, Map<String,String>> config = configService.findAllConfig();
		PageHelper.startPage(PageContext.getPageNum(),Integer.parseInt(config.get("后台第二类型分页").get("mSecondPage")));
		List<Secondtype> scs;
		scs=secondtypeMapper.findAllByFirsttypeId(nScId);
		
		PageInfo<Secondtype> pageInfo = new PageInfo<Secondtype>(scs);
		
		return pageInfo;
	}
	@Override
	public void addSecondtype(String name, Integer type1Id, String pic, String content, Admin admin) {
		// TODO Auto-generated method stub
		
		
		Date date=new Date();
		Timestamp inputTime = new Timestamp(date.getTime());
		Secondtype object=new Secondtype();
		object.setTitle(name);
	
		
		Firsttype ft=new Firsttype();
		ft.setId(type1Id);
		object.setFirsttype(ft);
		
		object.setInputTime(inputTime);
		object.setCheckTime(inputTime);
		object.setContent(content);
		 
		
	
		
		object.setAdmin(admin);
		Userinfo userinfo=new Userinfo();
		userinfo.setId(0);
		object.setUserinfo(userinfo);
		object.setIcon(pic);
		Integer maxId=secondtypeMapper.findMaxId();
		if(maxId==null) {
			maxId=0;
		}
		object.setSortCount(maxId+1);
		object.setEnable(true);
		object.setIsCheck(true);
		secondtypeMapper.insertSelective(object);
	}

	@Override
	public void userAddSecondtype(String objectName, Integer firsttypeId, String pic, String content,
			Userinfo userinfo) {
		// TODO Auto-generated method stub
		Date date=new Date();
		Timestamp inputTime = new Timestamp(date.getTime());
		Secondtype object=new Secondtype();
		object.setTitle(objectName);
	
		
		Firsttype ft=new Firsttype();
		ft.setId(firsttypeId);
		object.setFirsttype(ft);
		
		object.setInputTime(inputTime);
		
		object.setContent(content);
		object.setUserinfo(userinfo);
		object.setIcon(pic);
		object.setSortCount(0);
		object.setEnable(false);
		object.setIsCheck(false);
		secondtypeMapper.insertSelective(object);
	}
	@Override
	public Integer findMaxId() {
		// TODO Auto-generated method stub
		return secondtypeMapper.findMaxId();
	}

	@Override
	public void realDelete(Integer objectId) {
		// TODO Auto-generated method stub
		secondtypeMapper.deleteByPrimaryKey(objectId);
	}
	//根据一级分类查找二级分类列表 3.6 ad 
	@Override
	public List<Secondtype> findAllByFirsttypeId(Integer id) {
		// TODO Auto-generated method stub
		
		return secondtypeMapper.findAllByFirsttypeId(id);
	}

	@Override
	public Secondtype findBySecondtypeNameAndContent(String objectName, String content) {
		// TODO Auto-generated method stub
		return secondtypeMapper.findBySecondtypeNameAndContent(objectName, content);
	}

	@Override
	public void changeSortCount(Integer tid, Integer count) {
		
		secondtypeMapper.changeSortCount(tid,count);
		
	}

	@Override
	public boolean judgeIfRepetition(String objectName) {
		// TODO Auto-generated method stub
		Secondtype sc=secondtypeMapper.findByName(objectName);
		if(sc==null) {
			return false;
		}
		return true;
	}

	@Override
	public Map<Integer,Integer> getSonNum() {
		// TODO Auto-generated method stub
		Map<Integer,Integer> result=new HashMap<Integer,Integer>();
		List<Secondtype> scs=secondtypeMapper.findAll();
		for(Secondtype sc:scs) {
			result.put(sc.getId(),rubbishMapper.getCountByParentId(sc.getId()));
		}
		return result;
	}

	@Override
	public void updateSecondtypeById(Integer id, String name, Integer firsttypeId, String pic, String content, Admin admin) {
		Date date=new Date();
		Timestamp checkTime = new Timestamp(date.getTime());
		Secondtype object=new Secondtype();
		object.setTitle(name);
	
		
		Firsttype ft=new Firsttype();
		ft.setId(firsttypeId);
		object.setFirsttype(ft);
		object.setId(id);
		
		object.setCheckTime(checkTime);
		object.setContent(content);
		 
			
		if(pic==null||"".equals(pic)||"null".equals(pic)) {
			pic=secondtypeMapper.selectByPrimaryKey(id).getIcon();
			
		}
		object.setIcon(pic);
		System.out.println("object-Id:"+object.getId()+",object-content"+object.getContent());
		secondtypeMapper.updateByPrimaryKeySelective(object);
		
	}


}
